//
//  DataSummarySportRingCell.swift
//  TimeBoat
//
//  Created by bo wang on 2022/8/24.
//  Copyright © 2022 Time Boat. All rights reserved.
//

import UIKit

class DataSummartSportRing: ZYCircleProgressView {
    private lazy var dotView: UIView = {
        let v = UIView(frame: CGRect.init(x: 0, y: 0, width: 4, height: 4))
        v.backgroundColor = .white
        v.layer.cornerRadius = 2
        v.layer.masksToBounds = true
        v.isHidden = true
        return v
    }()
    
    override func layoutSubviews() {
        super.layoutSubviews()
        if dotView.superview != self {
            addSubview(dotView)
        }
        bringSubviewToFront(dotView)
    }
    
    override var progress: CGFloat {
        get {
            super.progress
        }
        set {
            super.progress = newValue

            let radius = bounds.width / 2
            let angle = newValue * Double.pi * 2
            let x = sin(angle) * radius
            let y = cos(angle) * radius
            let centerX = bounds.width / 2 + x
            let centerY = bounds.height / 2 - y
            dotView.center = CGPoint.init(x: centerX, y: centerY)
            dotView.isHidden = newValue < 0.02
        }
    }
}

class DataSummartSportRingsView: UIView {
    
    @IBOutlet var outerRing: DataSummartSportRing!
    @IBOutlet var innerRing: DataSummartSportRing!
    @IBOutlet var midRing: DataSummartSportRing!
    
    /// 外 中 内
    var data: (Double, Double, Double) = (0, 0, 0) {
        didSet {
            outerRing.progress = data.0
            midRing.progress = data.1
            innerRing.progress = data.2
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        outerRing.updateConfig { c in
            c?.backLineColor = HEX(0xFFFFFF, alpha: 0.2)
            c?.progressLineColor = .init(hex: 0xFFEF8A)
            c?.lineWidth = 7
        }
        midRing.updateConfig { c in
            c?.backLineColor = HEX(0xFFFFFF, alpha: 0.2)
            c?.progressLineColor = HEX(0x44ECFF)
            c?.lineWidth = 7
        }
        innerRing.updateConfig { c in
            c?.backLineColor = HEX(0xFFFFFF, alpha: 0.2)
            c?.progressLineColor = HEX(0xA8FF51)
            c?.lineWidth = 7
        }
    }
}

class DataSummarySportRingCell: DataSummaryCell {
    
    @IBOutlet var bgView: GradientView!
    @IBOutlet var ringView: DataSummartSportRingsView!
    
    @IBOutlet var distanceLabel: UILabel!
    @IBOutlet var calorieLabel: UILabel!
    @IBOutlet var stepsLabel: UILabel!
    @IBOutlet var ringStepsLabel: UILabel!
    
    override func update(vm: DataSummaryCellViewModel, data: Any?) {
        super.update(vm: vm, data: data)
        
        guard let steps = (data as? SummaryDataModel)?.totalStep else {
            showEmptyData()
            return
        }
        
        distanceLabel.text = String(format: "%.1f", steps.distance / 1000)
        calorieLabel.text = String(format: "%.2f", steps.calorie / 1000)
        stepsLabel.text = "\(steps.stepCount)"
        ringStepsLabel.text = "\(steps.stepCount)"
        
        ringView.data = (
            steps.distance / 5000,
            steps.calorie / 5000,
            Double(steps.stepCount ) / 5000
        )
    }
    
    override func showEmptyData() {
        distanceLabel.text = "--"
        calorieLabel.text = "--"
        stepsLabel.text = "--"
        ringStepsLabel.text = "0"
        ringView.data = (0, 0, 0)
    }
    
    @IBAction func onTapDistanceBtn(_ btn: UIButton) {
        DataSummaryVC.shared?.showChart(subject: .distance)
    }
    
    @IBAction func onTapCalorieBtn(_ btn: UIButton) {
        DataSummaryVC.shared?.showChart(subject: .calories)
    }
    
    @IBAction func onTapStepsBtn(_ btn: UIButton) {
        DataSummaryVC.shared?.showChart(subject: .steps)
    }
}
